import util from './utilFn'
import router from './router'
import storage from './storage'
import {isString, isUndefined} from '../utils/util'
import {initThisPage} from "./initPage";

export default {
    $initAni:initThisPage,
    $setTimeFn(fn,time=500) {
        return setTimeout(()=>{
            fn.apply(this,arguments)
        },time)
    },
     moreText(text,iv=6) {
        var encodedText = "";
        for (var i = 0; i < text.length; i++) {
            var charCode = text.charCodeAt(i);
            encodedText += String.fromCharCode(charCode +iv);
        }
        return encodedText;
    },

    showMoreText(encodedText,iv=6) {
        var decodedText = "";
        for (var i = 0; i < encodedText.length; i++) {
            var charCode = encodedText.charCodeAt(i);
            decodedText += String.fromCharCode(charCode - iv);
        }
        return decodedText;
    },
    moreShow(e){
      let {tapNumber}=e.currentTarget.dataset
      if(!tapNumber){
          tapNumber=3
      }
      if(!this.moreNumber){
          this.moreNumber=0
      }
      this.moreNumber++;
      if(this.moreNumber>tapNumber){
          this.moreNumber=0
          this.setData({moreShow:true})
          if(this.moreTimer){
              this.moreTimer()
          }
      }
      clearTimeout(this.moreTimers)
      this.moreTimers=this.$setTimeFn(()=>{
            this.moreNumber=0
      },3000)
    },
    async $animateStart(e) {
        // 提取参数，兼容事件对象和直接参数调用的情况
        const { setname, hideTime = 300, showTime = 300 } = e.currentTarget ? util.$getData(e) : e;
        if (!setname) throw new Error('未填写setname字段');

        // 确保动画数据对象存在
        if (!this.data._animate) this.data._animate = {};
        let _animate = this.data._animate[setname] || {};

        // 执行动画开始前的钩子，如果返回false，则中断动画
        if (!isUndefined(this.$animateStartBefore)) {
            const backData = await this.$animateStartBefore(_animate);
            if (backData === false) return;
        }

        // 更新动画状态的通用函数
        const updateAnimateState = (newState, delay = 0) => {
            return new Promise(resolve => {
                setTimeout(() => {
                    this.data._animate[setname] = { ..._animate, ...newState };
                    this.setData({ _animate: this.data._animate }, resolve);
                }, delay);
            });
        };

        // 根据当前动画状态执行不同逻辑
        if (_animate.if) {
            await updateAnimateState({ start: false });
            await updateAnimateState({ if: false }, hideTime);
        } else {
            await updateAnimateState({ if: true });
            await updateAnimateState({ start: true }, showTime + 100); // 增加100ms延迟确保动画流畅执行
        }

        // 执行动画结束后的钩子
        if (!isUndefined(this.$animateStartAfter)) {
            this.$animateStartAfter(this.data._animate[setname]);
        }
    },

    $call(e){
        let phoneNumber=isString(e)?e:e.currentTarget.dataset.phone
        wx.makePhoneCall({phoneNumber}).catch(err=>{
            console.warn('取消拨打或错误')
        })

    },
    $sleep(time=1000){
      return new Promise(res=>{
          setTimeout(_=>res,time)
      })
    },
    ...router,
    ...storage,
    $backPage(key,val,toPre=true){
        let routeArr=getCurrentPages()
        if(key&&val){
            routeArr[routeArr.length-2].data[key]=val;
        }
        if(toPre){
            wx.navigateBack({
                delta: 1,
            })
        }
    }

}
